package com.blog.servlet;

import java.io.IOException;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.blog.dao.ArticleDao;
import com.blog.dto.Article;

public class SearchServlet extends HttpServlet {

	
	protected void findAllArticle(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Vector<Article> vArticle=new Vector<Article>();
		ArticleDao articleDao=new ArticleDao();
		vArticle = articleDao.findAllArticle();
		HttpSession session = req.getSession();
		session.setAttribute("findArticle", vArticle);
		return ;
	}
	
	protected void searchAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String keyword = req.getParameter("keyword");
		System.out.println("keyword : "+keyword);
		Vector<Article> vArticle=new Vector<Article>();
		ArticleDao articleDao=new ArticleDao();
		vArticle = articleDao.findArticleByString(keyword);
		HttpSession session = req.getSession();
		session.setAttribute("findArticle", vArticle);
		return ;
	}
	
	protected void searchAuthor(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String keyword = req.getParameter("keyword");
		System.out.println("keyword : "+keyword);
		Vector<Article> vArticle=new Vector<Article>();
		ArticleDao articleDao=new ArticleDao();
		vArticle = articleDao.findArticleByAuthor(keyword);
		HttpSession session = req.getSession();
		session.setAttribute("findArticle", vArticle);
		return ;
	}
	protected void searchTitle(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String keyword = req.getParameter("keyword");
		System.out.println("keyword : "+keyword);
		Vector<Article> vArticle=new Vector<Article>();
		ArticleDao articleDao=new ArticleDao();
		vArticle = articleDao.findArticleByTitle(keyword);
		HttpSession session = req.getSession();
		session.setAttribute("findArticle", vArticle);
		return ;
	}
	
	protected void searchTag(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String keyword = req.getParameter("keyword");
		System.out.println("keyword : "+keyword);
		Vector<Article> vArticle=new Vector<Article>();
		ArticleDao articleDao=new ArticleDao();
		vArticle = articleDao.findArticleByTag(keyword);
		HttpSession session = req.getSession();
		session.setAttribute("findArticle", vArticle);
		return ;
	}
	protected void searchCategory(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String keyword = req.getParameter("keyword");
		System.out.println("keyword : "+keyword);
		Vector<Article> vArticle=new Vector<Article>();
		ArticleDao articleDao=new ArticleDao();
		vArticle = articleDao.findArticleByCategory(keyword);
		HttpSession session = req.getSession();
		session.setAttribute("findArticle", vArticle);
		return ;
	}
	protected void searchContent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String keyword = req.getParameter("keyword");
		System.out.println("keyword : "+keyword);
		Vector<Article> vArticle=new Vector<Article>();
		ArticleDao articleDao=new ArticleDao();
		vArticle = articleDao.findArticleByContent(keyword);
		HttpSession session = req.getSession();
		session.setAttribute("findArticle", vArticle);
		return ;
	}
	
	protected void searchKeyword(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String keyword = req.getParameter("keyword");
		System.out.println("keyword : "+keyword);
		Vector<Article> vArticle=new Vector<Article>();
		ArticleDao articleDao=new ArticleDao();
		vArticle = articleDao.findArticleByKeyword(keyword);
		HttpSession session = req.getSession();
		session.setAttribute("findArticle", vArticle);
		return ;
	}
	
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String act = null;
		act=req.getParameter("act");
		
		if(act==null || act.equals("")) act = "index";
		System.out.println("SearchServlet act = "+act);
		
		switch (act) {
		case "searchAll":
			searchAll(req, resp);
			//req.getRequestDispatcher("/home/searchSuccess.jsp").forward(req, resp);
			break;
		case "author":
			searchAuthor(req, resp);
			break;
		case "tag":
			searchTag(req, resp);
			break;
		case "category":
			searchCategory(req, resp);
			break;
		case "content":
			searchContent(req, resp);
			break;
		case "keyword":
			searchKeyword(req, resp);
			break;
		default:
			findAllArticle(req, resp);
//			页面重定向
//			resp.sendRedirect(req.getContextPath()+"/searchSuccess.jsp");
//			页面转发
//			req.getRequestDispatcher("/home/searchSuccess.jsp").forward(req, resp);
			break;
		}
		//resp.sendRedirect(req.getContextPath()+"/home/searchSuccess.jsp");
		req.getRequestDispatcher("/home/searchSuccess.jsp").forward(req, resp);
	}

}
